package com.battle.service.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.internal.OSSUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import nirvana.core.domains.vo.ApiResult;
import nirvana.core.exceptions.BizException;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

/**
 * @author anni.zhu
 * @author anni.zhu@king-v.com
 * Created in 2024/10/25 17:13
 */
@Slf4j
public class FileUtil {
    /**
     * 下载项目根目录下doc下的文件
     *
     * @param response response
     * @param fileName 文件名
     * @return 返回结果 成功或者文件不存在
     */
    public static ApiResult downloadFile(HttpServletResponse response, String fileName) {
        InputStream stream = FileUtil.class.getClassLoader().getResourceAsStream("static/excel/" + fileName);
//        InputStream stream = null;
//        try {
//            stream = new ClassPathResource("/static/excel/" + fileName).getInputStream();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        if (stream == null) {
            log.error("文件没有找到");
        }
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        try {
            String name = java.net.URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLDecoder.decode(name, "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            log.error("文件名转换编码出现问题:" + e.getMessage());
        }
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            bis = new BufferedInputStream(stream);
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.error("系统找不到指定的文件:" + e.getMessage());
            throw new BizException("系统找不到指定的文件");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("Io流写入出现错误:" + e.getMessage());
            throw new BizException("Io流写入出现错误");
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("关闭流出现错误:" + e.getMessage());
                    throw new BizException("关闭流出现错误");
                }
            }
        }
        return ApiResult.ok();
    }

    /**
     * 根据url获取文件
     *
     * @param fileURL
     * @return
     */
    public static InputStream getFileByUrl(String fileURL) {
        String filePath = fileURL;
        try {
            URL url=new URL(filePath);
            URLConnection urlConnection = url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            return inputStream;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("文件不存在");
        }
    }
}
